package com.hxd.mall.utils.local

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.text.TextUtils
import com.alibaba.android.arouter.facade.Postcard
import com.alibaba.android.arouter.facade.callback.NavCallback
import com.alibaba.android.arouter.launcher.ARouter
import com.hexin.locallibrary.permssion.PermissionGrantedCallback
import com.hexin.locallibrary.permssion.PermissionList
import com.hexin.locallibrary.permssion.PermissionUtils
import com.hxd.mall.R
import com.hxd.mall.constant.HxdConstants.IntentParamsKeyDefine.key_title_is_use
import com.hxd.mall.constant.HxdConstants.IntentParamsKeyDefine.key_title_name
import com.hxd.mall.constant.HxdConstants.IntentParamsKeyDefine.key_web_url
import com.hxd.mall.constant.HxdConstantsRouterPath.Common.path_to_webview
import com.hxd.mall.utils.open.ToastUtil
import com.hxd.mall.utils.open.saveBitmap

/**
 * 三个tab页面跳转
 */
object NavigationUtil {

    fun routerNavigation(path: String) {
        ARouter.getInstance().build(path).withTransition(R.anim.fade_in, R.anim.fade_out).navigation()
    }

    fun routerNavigation(path: String, activity: Activity, callback: () -> Unit) {
        ARouter.getInstance().build(path).withTransition(R.anim.fade_in, R.anim.fade_out).navigation(activity, object : NavCallback() {
            override fun onArrival(postcard: Postcard?) {
                callback()
            }
        })
    }

    /**
     * 带回调跳转
     */
    fun routerNavigation(path: String, map: MutableMap<String, Any>, activity: Activity, callback: () -> Unit) {
        ARouter.getInstance().build(path).withTransition(R.anim.fade_in, R.anim.fade_out).apply {
            map.forEach {
                when (it.value) {
                    is String -> this.withString(it.key, it.value.toString())
                    is Int -> this.withInt(it.key, it.value.toString().toInt())
                    is Long -> this.withLong(it.key, it.value.toString().toLong())
                    is Boolean -> this.withBoolean(it.key, it.value.toString().toBoolean())
                    else -> this.withObject(it.key, it.value)
                }
            }
        }.navigation(activity, object : NavCallback() {
            override fun onArrival(postcard: Postcard?) {
                callback()
            }
        })
    }


    /**
     * 跳转
     */
    fun routerNavigation(path: String, map: MutableMap<String, Any>) {
        ARouter.getInstance().build(path).withTransition(R.anim.fade_in, R.anim.fade_out).run {
            map.forEach {
                when (it.value) {
                    is String -> this.withString(it.key, it.value.toString())
                    is Int -> this.withInt(it.key, it.value.toString().toInt())
                    is Long -> this.withLong(it.key, it.value.toString().toLong())
                    is Boolean -> this.withBoolean(it.key, it.value.toString().toBoolean())
                    else -> this.withObject(it.key, it.value)
                }
            }
            navigation()
        }

    }

    fun routerNavigation(path: String, key: String, value: Any) {
        routerNavigation(path, mutableMapOf(key to value))
    }

    /**
     * 拨打电话（跳转到拨号界面，用户手动点击拨打）
     */
    fun callPhone(phone: String, context: Activity) {
        if (TextUtils.isEmpty(phone)) return
//        PermissionUtils.permissionCheck(context, arrayOf(android.Manifest.permission.CALL_PHONE), "", true, object : PermissionGrantedCallback {
//            override fun granted() {
//                val intent = Intent(Intent.ACTION_DIAL).apply {
//                    data = Uri.parse("${context.getString(R.string.tel_phone_about_start_name)}$phone")
//                }
//                context.startActivity(intent)
//            }
//            override fun denied() {
//                ToastUtil.showToast(context.getString(R.string.permission_tip))
//            }
//        })

        PermissionUtils.permissionCheck(context, arrayOf(android.Manifest.permission.CALL_PHONE), "", object : PermissionGrantedCallback {
            override fun granted() {
                val intent = Intent(Intent.ACTION_DIAL).apply {
                    data = Uri.parse("${context.getString(R.string.tel_phone_about_start_name)}$phone")
                }
                context.startActivity(intent)
            }

            override fun denied() {
                ToastUtil.showToast(context.getString(R.string.permission_tip))
            }
        })
    }

    fun jumpToWebView(title: String, url: String) {
        routerNavigation(
            path_to_webview, mutableMapOf(key_title_name to title, key_title_is_use to true, key_web_url to url)
        )
    }
}


